package cn.iocoder.yudao.module.erp.controller.admin.warehouseoutlist;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.warehouseoutlist.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.warehouseoutlist.WarehouseOutListDO;
import cn.iocoder.yudao.module.erp.service.warehouseoutlist.WarehouseOutListService;

@Tag(name = "管理后台 - ERP 出库单表（一二级库）")
@RestController
@RequestMapping("/erp/warehouse-out-list")
@Validated
public class WarehouseOutListController {

    @Resource
    private WarehouseOutListService warehouseOutListService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 出库单表（一二级库）")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-out-list:create')")
    public CommonResult<Long> createWarehouseOutList(@Valid @RequestBody WarehouseOutListSaveReqVO createReqVO) {
        return success(warehouseOutListService.createWarehouseOutList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 出库单表（一二级库）")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-out-list:update')")
    public CommonResult<Boolean> updateWarehouseOutList(@Valid @RequestBody WarehouseOutListSaveReqVO updateReqVO) {
        warehouseOutListService.updateWarehouseOutList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 出库单表（一二级库）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:warehouse-out-list:delete')")
    public CommonResult<Boolean> deleteWarehouseOutList(@RequestParam("id") Long id) {
        warehouseOutListService.deleteWarehouseOutList(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 出库单表（一二级库）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-out-list:query')")
    public CommonResult<WarehouseOutListRespVO> getWarehouseOutList(@RequestParam("id") Long id) {
        WarehouseOutListDO warehouseOutList = warehouseOutListService.getWarehouseOutList(id);
        return success(BeanUtils.toBean(warehouseOutList, WarehouseOutListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 出库单表（一二级库）分页")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-out-list:query')")
    public CommonResult<PageResult<WarehouseOutListRespVO>> getWarehouseOutListPage(@Valid WarehouseOutListPageReqVO pageReqVO) {
        PageResult<WarehouseOutListDO> pageResult = warehouseOutListService.getWarehouseOutListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarehouseOutListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 出库单表（一二级库） Excel")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-out-list:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarehouseOutListExcel(@Valid WarehouseOutListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarehouseOutListDO> list = warehouseOutListService.getWarehouseOutListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 出库单表（一二级库）.xls", "数据", WarehouseOutListRespVO.class,
                        BeanUtils.toBean(list, WarehouseOutListRespVO.class));
    }

}